<template>
  <div class="app-container">
    <div class="form">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               status-icon
               label-width="90px">
        <el-input type="hidden"></el-input>
        <el-form-item label="头像">
          <el-upload class="avatar-uploader"
                     action="http://localhost:5001/api/my/photo"
                     :headers="headers"
                     :show-file-list="false"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="form.Photo"
                 :src="photoUrl"
                 class="avatar">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.Name"
                    placeholder="姓名"
                    prefix-icon="el-icon-edit"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号码"
                      prop="TelPhone">
          <el-input v-model="form.TelPhone"
                    placeholder="手机号码"
                    prefix-icon="el-icon-mobile-phone"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱"
                      prop="Email">
          <el-input v-model="form.Email"
                    placeholder="电子邮箱"
                    prefix-icon="el-icon-message"
                    clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     :loading="formLoading"
                     @click="onSubmit">更新</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { get, update } from '@/api/my'
import { getToken } from '@/utils/auth'
import rules from '@/utils/rules'

export default {
  name: 'info',
  data () {
    return {
      headers: {
        Authorization: "Bearer " + getToken()
      },
      photoUrl: '',
      formLoading: false,
      form: {
        Id: '',
        Photo: '',
        TelPhone: '',
        Name: '',
        Phone: '',
        Email: ''
      },
      formRules: {
        TelPhone: [
          {
            pattern: /^1[34578]\d{9}$/,
            message: '目前只支持中国大陆的手机号码',
            trigger: 'blur'
          }
        ],
        Email: [
          {
            pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
            message: '请输入正确的邮箱号码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getMyInfo()
  },
  methods: {
    getMyInfo () {
      let that = this
      get()
        .then(response => {
          const { Data } = response
          if (!Data) {
            reject('获取信息失败, 请重新登录.')
          }
          that.form = Data
          that.photoUrl = process.env.VUE_APP_BASE_API + "/" + this.form.Photo
        })
    },
    handleAvatarSuccess (res, file) {
      console.log(process.env.VUE_APP_BASE_API);
      this.form.Photo = res.Data
      this.photoUrl = process.env.VUE_APP_BASE_API + "/" + res.Data
      this.$message({
        message: '上传成功',
        type: 'success'
      })
      // console.log(res)
      // console.log(file)
      // console.log(file.raw)
      //URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isGIF = file.type === 'image/gif';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG && !isGIF) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
        return false
      }
      return true;
    },
    onSubmit () {
      this.formLoading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          update(this.form).then(response => {
            this.$message({
              message: response.Message,
              type: 'success'
            })
            this.formLoading = false
          })
        } else {
          console.log('error submit!!');
          this.formLoading = false
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.form {
  width: 420px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
